﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetAppBlocks.Base;
using ProgNetDxComponents.ExtenderProviders.Browsers;
using ProgNet.Constants;
using DevExpress.XtraBars;
using ProgNetDxComponents.ExtenderProviders.Options;
using ProgNetAppBlocks.Forms;

namespace MiniKsiegowosc
{
    public partial class MiniKsiegowoscMain : ProgNetForm
    {
        public MiniKsiegowoscMain()
        {
            InitializeComponent();
            if (this.IsAppRunning())
            {
                xtraTabbedMdiManager1.SetMinTabSize(100);
                xtraTabbedMdiManager1.AddCloseTabMenu();
                OptionCache.OptionChanged += new EventHandler<OptionChangedEventArgs>(OptionCache_OptionChanged);
                MergeMenus = OptionCache.GetOptionBoolValue(OptionNames.MergeMenus);
                SaveLayoutOnExit = OptionCache.GetOptionBoolValue(OptionNames.AutoSaveLayout);
            }
        }

        private Browser LataPodatkoweBrowser
        {
            get { return BrowserCache.GetBrowser(BrowserNames.RokPodatkowy, false); }
        }
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           // ObjectDialogResult dlg = BrowserCache.GetBrowser(ProgNet.Constants.BrowserNames.Kontrachenci, false).ShowDialog();
           // MiniKsiegowosc.RaportTools.FormMonthRaportPrint f= new MiniKsiegowosc.RaportTools.FormMonthRaportPrint();
           // f.ShowDialog();
        }
        private bool MergeMenus = false;
        private bool SaveLayoutOnExit = false;
        void OptionCache_OptionChanged(object sender, OptionChangedEventArgs e)
        {
            #region Menu merge
            if ((e.OptionNames != null) && e.OptionNames.Contains(OptionNames.MergeMenus))
            {
                if (MergeMenus != OptionCache.GetOptionBoolValue(OptionNames.MergeMenus))
                {
                    MergeMenus = OptionCache.GetOptionBoolValue(OptionNames.MergeMenus);
                    if (!MergeMenus)
                    {
                        if (this.ActiveMdiChild != null)
                        {
                            foreach (Control c in this.ActiveMdiChild.Controls)
                            {
                                if (c is BarDockControl)
                                {
                                    barManager1_UnMerge(this, new BarManagerMergeEventArgs((c as BarDockControl).Manager));
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (this.ActiveMdiChild != null)
                        {
                            foreach (Control c in this.ActiveMdiChild.Controls)
                            {
                                if (c is BarDockControl)
                                {
                                    barManager1_Merge(this, new BarManagerMergeEventArgs((c as BarDockControl).Manager));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            if ((e.OptionNames!=null) && e.OptionNames.Contains(OptionNames.AutoSaveLayout))
            {
                SaveLayoutOnExit = OptionCache.GetOptionBoolValue(OptionNames.AutoSaveLayout);
                foreach(Form f in this.MdiChildren){
                    if (f is ProgNetSpreadSheetForm)
                    {
                        try
                        {
                            (f as ProgNetSpreadSheetForm).SaveLayoutOnExit = SaveLayoutOnExit;
                        }
                        catch { }
                    }
                }
            }
        }

        private void barManager1_Merge(object sender, BarManagerMergeEventArgs e)
        {
            if (MergeMenus)
            {
                if (e.ChildManager.Bars["Tools"] != null)
                {
                    e.ChildManager.Bars["Tools"].Visible = false;
                    foreach (BarItemLink l in e.ChildManager.Bars["Tools"].ItemLinks)
                        barManager1.Bars["Tools"].AddItem(l.Item);
                }
                barManager1.Bars["Tools"].Visible = barManager1.Bars["Tools"].ItemLinks.Count > 0;
            }
        }

        private void barManager1_UnMerge(object sender, BarManagerMergeEventArgs e)
        {
            if (e.ChildManager.Bars["Tools"] != null)
                e.ChildManager.Bars["Tools"].Visible = true;
            barManager1.Bars["Tools"].ItemLinks.Clear();
            barManager1.Bars["Tools"].Visible = false;
        }
        #region Ustawienia
        private void bbiKontrachenci_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.FormKontrachenci));
        }
        
        #region Ksiegi
        private void bbiStpKpir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.KPIR.FormKpir));
        }
        private void bbiStpVatS_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.VatS.FormVatS));
        }
        private void bbiStpVatZ_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.VatZ.FormVatZ));
            // mdiHelper1.GetMdiChild(typeof(Form1));
        }

        #endregion
        private void bbiLataPodatkowe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.RokKsiegowy.FormRokKsiegowy));
        }

        #endregion

        #region Ksiegi
        private void bbiWrkKpirObecny_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.KPIR.FormWrkKpir), true, DateTime.Now.Year);
        }
        private void bbiWrkKpirZeszły_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ObjectDialogResult dlg = LataPodatkoweBrowser.ShowDialog();
            if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK && dlg.Value.IsNotNull())
            {
                mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.KPIR.FormWrkKpir), true, dlg.Value.AsInt());
            }
        }

        private void bbiWrkVatSprzedazObecne_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.VatS.FormWrkVatS), true, DateTime.Now.Year);
        }
        private void bbiWrkVatSprzedazZeszle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ObjectDialogResult dlg = LataPodatkoweBrowser.ShowDialog();
            if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK && dlg.Value.IsNotNull())
            {
                mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.VatS.FormWrkVatS), true, dlg.Value.AsInt());
            }
        }
        
        private void bbiWrkVatZObecny_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.VatZ.FormWrkVatZ), true, DateTime.Now.Year);
        }
        private void bbiWrkVatZZeszle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ObjectDialogResult dlg = LataPodatkoweBrowser.ShowDialog();
            if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK && dlg.Value.IsNotNull())
            {
                mdiHelper1.GetMdiChild(typeof(Wrk.Ksiegi.VatZ.FormWrkVatZ), true, dlg.Value.AsInt());
            }
        }
        #endregion

        private void mdiHelper1_FormCreated(object sender, EventArgs e)
        {
            try
            {
                ProgNetAppBlocks.Forms.ProgNetSpreadSheetForm f = sender as ProgNetAppBlocks.Forms.ProgNetSpreadSheetForm;
                if (f != null)
                    f.SaveLayoutOnExit = OptionCache.GetOptionBoolValue(OptionNames.AutoSaveLayout);
            }
            catch { }
        }

        private void bbiOpcje_ItemClick(object sender, ItemClickEventArgs e)
        {
            mdiHelper1.GetMdiChild(typeof(Ustawienia.FormOpcje));
        }
        
        
    }
}
